package cn.net.xyan.easy.spring.testwar.gwt.client.ui;


import cn.net.xyan.easy.gwt.client.EZGwt;
import cn.net.xyan.easy.gwt.client.model.MenuItemModel;
import cn.net.xyan.easy.gwt.client.model.TableCellDataModel;
import cn.net.xyan.easy.gwt.client.ui.MainFrame;

import cn.net.xyan.easy.gwt.client.ui.TableDelegate;
import cn.net.xyan.easy.gwt.client.ui.cell.ButtonItemCell;
import cn.net.xyan.easy.gwt.shared.StringUtils;
import cn.net.xyan.easy.webapp.base.api.dto.QueryWebModuleRsq;

import cn.net.xyan.easy.webapp.base.frontend.service.SystemRootAsync;
import cn.net.xyan.easy.webapp.base.frontend.ui.UserView;
import com.google.gwt.core.client.GWT;

import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;

import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.view.client.ListDataProvider;

import org.fusesource.restygwt.client.Method;
import org.fusesource.restygwt.client.MethodCallback;
import org.gwtbootstrap3.client.ui.constants.ButtonType;
import org.gwtbootstrap3.client.ui.constants.IconType;
import org.gwtbootstrap3.extras.bootbox.client.Bootbox;

import java.util.Arrays;
import java.util.List;


/**
 * Created by zarra on 16/2/18.
 */
public class Index extends MainFrame {
    @Override
    protected Panel setupUI() {

        //Defaults.setServiceRoot(EZGwt.appFullRoot());

        Panel panel = super.setupUI();

        getNavbar().setTitle("New Hello world");

        String root = EZGwt.appRoot();


        SystemRootAsync scca=  EZGwt.create(SystemRootAsync.class);

        scca.queryModules(new MethodCallback<List<QueryWebModuleRsq>>() {
            @Override
            public void onFailure(Method method, Throwable exception) {

            }

            @Override
            public void onSuccess(Method method, List<QueryWebModuleRsq> response) {
                for (QueryWebModuleRsq webModuleRsq : response) {
                    MenuItemModel model = new MenuItemModel();
                    model.setTitle(webModuleRsq.getTitle());
                    for (QueryWebModuleRsq item : webModuleRsq.getItems()) {
                        MenuItemModel modelItem = new MenuItemModel(item.getTitle());
                        GWT.log(item.getUrl());
                        model.getItems().add(modelItem);
                    }
                    getSideBar().addItem(model);
                }
            }
        });




        ListDataProvider<TableCellDataModel> provider = new ListDataProvider<>();


        for (int i = 0; i < 100; i++) {
            final int finalI = i;
            provider.getList().add(new TableCellDataModel() {
                @Override
                public Class<?> classForColumnIndex(int index) {
                    return String.class;
                }

                @Override
                public <T> T valueForColumnIndex(Class<T> tClass, int index) {
                    return (T) StringUtils.format("value {1}-{0}", index + 1, finalI + 1);
                }
            });
        }

        provider.flush();

        TableDelegate<TableCellDataModel> delegate = new TableDelegate<TableCellDataModel>() {
            @Override
            public int numberOfColumn() {
                return 10;
            }

            @Override
            public Column<TableCellDataModel, ?> column(final int columnIndex) {
                return new TextColumn<TableCellDataModel>() {
                    @Override
                    public String getValue(TableCellDataModel object) {
                        return object.valueForColumnIndex(String.class, columnIndex);
                    }
                };
            }

            @Override
            public String columnName(int columnIndex) {
                return StringUtils.format("Field {0}",columnIndex);
            }

            @Override
            public List<ButtonItemCell<TableCellDataModel>> buttons() {
                return Arrays.<ButtonItemCell<TableCellDataModel>>asList(
                        new ButtonItemCell<TableCellDataModel>("Delete", ButtonType.DANGER, IconType.REMOVE) {
                            @Override
                            public void onAction(int index, TableCellDataModel object, String value) {
                                Bootbox.alert("remove");
                            }
                        },
                        new ButtonItemCell<TableCellDataModel>("Edit", ButtonType.PRIMARY, IconType.EDIT) {
                            @Override
                            public void onAction(int index, TableCellDataModel object, String value) {
                                Bootbox.alert("Edit");
                            }
                        }
                );
            }
        };

        UserView userView = new UserView();
        userView.setHeight("100%");
        ScrollPanel scrollPanel = new ScrollPanel(userView);
        scrollPanel.setHeight("100%");
        setContentWidget(scrollPanel);




        return panel;
    }
}
